/*
 * JSelectClassroom.java
 *
 * Created on 22 maj 2008, 18:26
 */

package schoolmanagement.dialogs;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import schoolmanagement.controller.DBAccess;
import schoolmanagement.entity.SmClassroom;

/**
 *
 * @author  deely
 */
public class JSelectClassroom extends javax.swing.JDialog {
    
    private SmClassroom m_result;
    
    public SmClassroom getResult()
    {
        return m_result;
    }
    
    /** Creates new form JSelectClassroom */
    public JSelectClassroom(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        m_result = null;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtCancel = new javax.swing.JButton();
        jbtSelect = new javax.swing.JButton();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTblRoleUsers = new javax.swing.JTable();
        jTbName2 = new javax.swing.JTextField();
        jBtnSearch2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jbtCancel.setText("Anuluj"); // NOI18N
        jbtCancel.setName("jbtCancel"); // NOI18N
        jbtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCancelActionPerformed(evt);
            }
        });

        jbtSelect.setText("Wybierz"); // NOI18N
        jbtSelect.setName("jbtSelect"); // NOI18N
        jbtSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSelectActionPerformed(evt);
            }
        });

        jScrollPane7.setName("jScrollPane7"); // NOI18N

        jTblRoleUsers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numer sali", "Sala"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTblRoleUsers.setName("jTblRoleUsers"); // NOI18N
        jTblRoleUsers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTblRoleUsersmouseClicked(evt);
            }
        });
        jScrollPane7.setViewportView(jTblRoleUsers);

        jTbName2.setName("jTbName2"); // NOI18N
        jTbName2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTbName2FocusGained(evt);
            }
        });

        jBtnSearch2.setText("Szukaj"); // NOI18N
        jBtnSearch2.setName("jBtnSearch2"); // NOI18N
        jBtnSearch2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSearch2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 503, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTbName2, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBtnSearch2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jbtSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 197, Short.MAX_VALUE)
                        .addComponent(jbtCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 405, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnSearch2)
                    .addComponent(jTbName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtSelect)
                    .addComponent(jbtCancel)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCancelActionPerformed
        m_result = null;
        setVisible(false);
    }//GEN-LAST:event_jbtCancelActionPerformed

    private void jbtSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSelectActionPerformed
        if( jTblRoleUsers.getSelectedRowCount() != 1 ) return;
        int row = jTblRoleUsers.getSelectedRow();
        Object o = jTblRoleUsers.getValueAt(row, 0);
        if(o == null) return;
        SmClassroom classroom = (SmClassroom)jTblRoleUsers.getValueAt(row, 0);
        m_result = classroom;
        setVisible(false);
    }//GEN-LAST:event_jbtSelectActionPerformed

    private void jTblRoleUsersmouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTblRoleUsersmouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            JTable target = (JTable)evt.getSource();
            int row = target.getSelectedRow();
            Object o = target.getValueAt(row, 0);
            if(o == null)
                return;
            SmClassroom classroom = (SmClassroom)target.getValueAt(row, 0);
            m_result = classroom;
            setVisible(false);
        }
    }//GEN-LAST:event_jTblRoleUsersmouseClicked

    private void jTbName2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTbName2FocusGained
        
    }//GEN-LAST:event_jTbName2FocusGained

    private void jBtnSearch2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSearch2ActionPerformed
        // TODO add your handling code here:
        
        
        
        jTblRoleUsers.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
        },
                new String [] {
            "Nazwisko i Imię", "Grupa"
        }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };
            
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        DefaultTableModel model = (DefaultTableModel) jTblRoleUsers.getModel();
        
        List<SmClassroom> list = new ArrayList<SmClassroom>();
        list = DBAccess.GetInstance().getClassroomByName(jTbName2.getText().trim());
        
        for(SmClassroom classroom : list){
            model.addRow(new Object[]{classroom, classroom.getClrDescr()});
        }
    }//GEN-LAST:event_jBtnSearch2ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JSelectClassroom dialog = new JSelectClassroom(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnSearch2;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JTextField jTbName2;
    private javax.swing.JTable jTblRoleUsers;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtSelect;
    // End of variables declaration//GEN-END:variables

    
}
